package com.musiklegal;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Profile extends Activity 
{
	static String username;
	Button message;
	ImageView profpict;
	TextView recent,genre,nama;
	ListView lv;
	Handler h;
	List<DataSong> list;
	InputStream is;
	String status,name, judul,aliran;
	ProgressDialog dialog;
	
	 protected Dialog onCreateDialog(int id) {
	        switch (id) {
	              case 0: {
	                    dialog = new ProgressDialog(this);
	                    dialog.setMessage("Loading...");
	                    dialog.setIndeterminate(true);
	                    dialog.setCancelable(true);
	                    return dialog;                   
	              }
	        }
	        return null;
	  }
	
	public void onCreate(Bundle save)
	{
		h = new Handler();
		super.onCreate(save);
		setContentView(R.layout.profilefriend);
		showDialog(0);
		message = (Button) findViewById(R.id.sendmess);
		profpict = (ImageView) findViewById(R.id.profpict);
		lv = (ListView) findViewById(R.id.listSong);
		recent = (TextView) findViewById(R.id.recentplay);
		genre = (TextView) findViewById(R.id.texttitle);
		nama = (TextView) findViewById(R.id.textnama);
		list = new ArrayList<DataSong>();
		InterfaceAdapter adapter = new InterfaceAdapter(this,list);
		lv.setAdapter(adapter);	
		message.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				Send.usernameto = username;
				startActivity(new Intent(Profile.this,Send.class));
				
			}});
		lv.setOnItemClickListener(new OnItemClickListener() 
        {
	          public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
	          {
	        	  DataSong d = (DataSong) lv.getItemAtPosition(position);
	        	  Log.d("url", d.album);
	        	  TrackInfo.isCatalogue = false;
	        	  try{
	        		 
	        		  	TrackInfo.ds =d;
	        		  	
	        			 TrackInfo.fromCatalogue=true;
	        		  	TrackInfo.isCatalogue=true;
	        			 TrackInfo.fromFriend=true;
	        			 TrackInfo.mercapi=d.api;
	        			 System.gc();
	        			 
	        			 Intent i = new Intent(getApplicationContext(), TrackInfo.class);
	        			 startActivity(i);
	        	  }
	        	  catch (Exception e) {
	              }
	        	  }
	        });
		
		new Thread(new Runnable(){

			public void run() {
				
				try
				{
					HttpClient tinfo = new DefaultHttpClient(); 
					HttpGet gets= new HttpGet("http://secure.musiklegal.com/?action=socialnet&method=friend_top_play&username="+username);
					HttpResponse hrep = tinfo.execute(gets);
					HttpEntity trinfo = hrep.getEntity();
					SAXParserFactory spf=SAXParserFactory.newInstance();
					final FriendParser cp = new FriendParser();
					SAXParser sp=spf.newSAXParser() ;
					XMLReader xr = sp.getXMLReader();
					xr.setContentHandler(cp);
					xr.parse(new InputSource(trinfo.getContent()));
					
					
					URL  u = new URL(cp.packer.picture);
					is =u.openConnection().getInputStream();
					
					list.addAll( cp.packer.songlist);
					status = cp.packer.status;
					judul = cp.packer.fullname+"'s recently play";
					aliran = cp.packer.genre;
					h.post(new Runnable(){

						public void run() {
							removeDialog(0);
							profpict.setImageBitmap(BitmapFactory.decodeStream(is));
							nama.setText(cp.packer.fullname);
							recent.setText(status.substring(8));
							setTitle(judul);
							genre.setText(aliran);
							lv.invalidateViews();
							
						}});
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}
				catch (ParserConfigurationException e) 
				{
					
					e.printStackTrace();
				} 
				catch (SAXException e) 
				{
					
					e.printStackTrace();
				}
				
				
			}}).start();
		
		
	}
}
